package net.adetech.crj.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.WgrtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.dao.ApplyMapper;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.User;
import net.adetech.crj.entity.tb.*;
import net.adetech.crj.utils.ApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("wgrtb.submit")
public class WgrtbSubmitService implements BaseService {

    @Autowired
    ApiUtils apiUtils;
    @Autowired
    ApplyMapper applyMapper;

    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        String ywbh = apiUtils.getYwbh();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        WgrtbForm form = (WgrtbForm) params.get("form");
        User user = (User) params.get("user");
        if (user==null)
        {
            user = new User();
            user.setIdNumber("null");
        }


        String args = this.getArgs(ywbh,form);
        System.out.println(args);

        JSONObject response = apiUtils.call("sq_savePreApply",args);
        List<Apply> originalApplyList = applyMapper.getApplyListByIdNumberAndApplyUserId(form.getXczjhm(), user.getIdNumber());

        //原预约id
        Long originalAppointmentId = null;
        if (originalApplyList!=null&& originalApplyList.size()>0)
        {
            originalAppointmentId = originalApplyList.get(0).getAppointmentId();
        }



        applyMapper.updateApplyStateByIdNumberAndApplyUserId(form.getXczjhm(), user.getIdNumber(), "0");
        Apply apply = new Apply();
        apply.setApplyUserId(user.getIdNumber());
        apply.setRealName(form.getYwm() +" "+ form.getYwx());
        apply.setIdNumber(form.getXczjhm());
        apply.setIdType("2");
        apply.setApplySummary("B");
        apply.setApplyDetail(JSON.toJSONString(form));
        apply.setApplyNo(ywbh);
        apply.setPhoneNumber(form.getJnlxdh());
        apply.setRemindJson("[]");

        applyMapper.saveApply(apply);
        apply = applyMapper.getApplyByYwbh(ywbh);


        resultMap.put("ywbh",ywbh);
        resultMap.put("args",args);
        resultMap.put("response",response);
        resultMap.put("apply",apply);


        return resultMap;
    }


    private String getArgs(String ywbh,WgrtbForm form)
    {

        String dayId = new SimpleDateFormat("yyyyMMdd").format(new Date());
        SqSavePreApplyArgs args = new SqSavePreApplyArgs();

        args.setSqlb(form.getSqlb());

        SqxxInfo sqxxInfo = new SqxxInfo();
        args.setInfo(sqxxInfo);

        TbjWgr wgrsqxx = new TbjWgr();
        sqxxInfo.setWgrsqxx(wgrsqxx);
        wgrsqxx.setYwbh(ywbh);
        wgrsqxx.setYwx(form.getYwx());
        wgrsqxx.setYwm(form.getYwm());
        wgrsqxx.setZwxm(form.getZwxm());
        wgrsqxx.setJnlxdh(form.getJnlxdh());
        wgrsqxx.setGjdq(form.getGjdq());
        wgrsqxx.setCsrq(form.getCsrq());
        wgrsqxx.setCsd(form.getCsd());
        wgrsqxx.setXb(form.getXb());
        wgrsqxx.setJnjzdxxdz(form.getJnjzdxxdz());
        wgrsqxx.setDbr(form.getDbr());
        wgrsqxx.setDbrlxdh(form.getDbrlxdh());
        wgrsqxx.setXcqzzl(form.getXcqzzl());
        wgrsqxx.setXcqzhm(form.getXcqzhm());
        wgrsqxx.setXczjyxqz(form.getXczjyxqz());
        wgrsqxx.setXcqzzl(form.getXcqzzl());
        wgrsqxx.setXcqzhm(form.getXcqzhm());
        wgrsqxx.setXctlyxqz(form.getXctlyxqz());
        wgrsqxx.setSqlb(form.getSqlb());

        wgrsqxx.setSqqzzl(form.getSqqzzl());
        wgrsqxx.setJlxksy(form.getJlxksy());
        wgrsqxx.setBzlx(form.getBzlx());
        wgrsqxx.setQzyxqz(form.getQzyxqz());
        wgrsqxx.setYwbh(ywbh);
        wgrsqxx.setXczjzl(form.getXczjzl());
        wgrsqxx.setXczjhm(form.getXczjhm());
        wgrsqxx.setXczjyxqz(form.getXczjyxqz());


        wgrsqxx.setSlrq(dayId);


        return JSON.toJSONString(args);
    }

}
